package com.example.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.example.service.ISMSService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @创建人 liangzai
 * @创建时间 2022/10/21
 * @描述 短信平台接口
 */
@Slf4j
@Service
public class SMSServiceImpl implements ISMSService {


    private static final String  userName = "cfwx";
    private static final String passWd = "e10adc3949ba59abbe56e057f20f883e";

    /**
     * 发送短信息
     *
     * @param phone   手机号
     * @param message 短信内容
     * @return 状态  0-成功 1-失败
     */
    @Override
    public int sendMessage(String phone, String message) {
        log.info("【发送短信息接口请求参数: 手机号 :{} ,消息内容:{}】", phone, message);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("lx", "1");
        hashMap.put("userName", userName);
        hashMap.put("passWd", passWd);
        hashMap.put("mobile", "15701579470");
        hashMap.put("mobileNum", "1");
        hashMap.put("content", "内容");
        hashMap.put("sendLevel", "4");
        hashMap.put("tunnelFlowId", "1");
        hashMap.put("tunnelChannelType", "1");
        String messageUrl = "http://202.110.117.34:10002";
        log.info("【请求第三方短信接口 URL = {} ，请求参数 = {}】", messageUrl, JSON.toJSONString(hashMap));
        String result = HttpUtil.post(messageUrl, HttpUtil.toParams(hashMap));
        log.info("【第三方短信接口 响应结果 = {}】", JSON.toJSONString(result));
        if (StrUtil.isBlank(result)) {
            throw new RuntimeException("【第三方短信接口返回结果异常】");
        }
        //lx1,0,1000,2210241739380000091
        String[] split = result.split(",");
        int statusCode = Integer.valueOf(split[2]);
         if (1000 == statusCode) {
            //成功
            return 0;
        }
        return 1;
    }
}
